package br.upe.dsc.caeto.ui;

import java.awt.Color;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.swing.JPanel;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import br.upe.dsc.caeto.simulation.Result;

public class ChartBuilder {
	private XYSeries series;
	private Map<String, XYSeries> seriesMap;
	private Image ballImage;
	private Image melhorValorIcon;
	private Image melhorValorIconMaior;
	private Image melhorValorIconMenor;
	private String eixoX;
	private String eixoY;
	private int altura;
	private int largura;
	private int graficoAtual = 1;
	private Shape[] pontosAtuais;
	private float melhorValorCusto;

	private Color[] cores = { Color.GREEN, Color.YELLOW, Color.BLUE,
			Color.ORANGE, Color.CYAN, Color.PINK, Color.GRAY, Color.MAGENTA };

	private Shape[] shapesMenores = { new Rectangle(0, 0, 6, 6),
			new Ellipse2D.Double(0, 0, 6, 4), new Rectangle(0, 0, 4, 6),
			new Rectangle(0, 0, 6, 4), new Rectangle(0, 0, 6, 6),
			new Rectangle(0, 0, 4, 6), new Rectangle(0, 0, 6, 6),
			new Rectangle(0, 0, 4, 6), new Ellipse2D.Double(0, 0, 6, 6) };

	private Shape[] shapesMaiores = { new Rectangle(0, 0, 6, 8),
			new Ellipse2D.Double(0, 0, 8, 4), new Rectangle(0, 0, 6, 8),
			new Rectangle(0, 0, 8, 6), new Rectangle(0, 0, 8, 8),
			new Rectangle(0, 0, 6, 8), new Rectangle(0, 0, 6, 8),
			new Rectangle(0, 0, 8, 8), new Ellipse2D.Double(0, 0, 8, 8) };

	private final int NUMERO_IMAGENS = cores.length;

	public int funcaoCusto(XYSeries serie) {
		float melhorValor;
		float valor;
		int melhorIndice;

		melhorValor = Float.MAX_VALUE;
		melhorIndice = -1;
		for (int i = 0; i < serie.getItemCount(); i++) {
			valor = serie.getX(i).floatValue() * serie.getY(i).floatValue();
			if (valor < melhorValor) {
				melhorValor = valor;
				melhorIndice = i;
			}
		}
		this.melhorValorCusto = melhorValor;

		return melhorIndice;
	}

	public void mostrarDados(String name, List<Result> results) {
		double x;
		double y;
		int melhorIndiceSerie;
		List<XYSeries> seriesList = new ArrayList<XYSeries>();

		if (name.length() > 4) {
			this.series = new XYSeries(name.substring(0, name.length() - 4));
		}
		
		this.eixoX = "Energy"; // Nome para o eixo X
		this.eixoY = "Cycles"; // Nome para o eixo Y

		//FIX IT!
		/*for (Result result : results) {
			this.series.add(result.getTotalEnergy(), result.getTotalCycles());
		}*/

		seriesList.add(this.series);
		seriesMap.put(name, this.series);
		melhorIndiceSerie = this.funcaoCusto(this.series);
		//montaGrafico(nome, pontosAtuais[(graficoAtual - 1)
		//                 % NUMERO_IMAGENS], melhorIndiceSerie, 0, seriesList);
		graficoAtual++;
	}
	
	private void montaGrafico(String titulo, Shape shape, int melhorIndice,
			int melhorSerie, List<XYSeries> seriesList) {
		XYPlot plot;
		final NumberAxis domainAxis = new NumberAxis(this.eixoX);
		final NumberAxis rangeAxis = new NumberAxis(this.eixoY);
		final SimpleRenderer renderer = new SimpleRenderer();
		final JFreeChart chart;
		final ChartPanel chartPanel;
		Image plotIcons[];
		JPanel panelTemporario = new JPanel();
		/*Add layout*/

		renderer.setPlotImages(true);
		renderer.setPlotIcon(this.ballImage);
		renderer.setBestPlotIcon(this.melhorValorIcon);
		renderer.setMelhorSerie(melhorSerie);
		renderer.setMelhorindice(melhorIndice);
		renderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator());
		renderer.setPlotColor(cores[(graficoAtual - 1) % NUMERO_IMAGENS]);
		renderer.setPlotShape(shape);
		plot = new XYPlot(null, domainAxis, rangeAxis, renderer);
		plotIcons = new Image[seriesList.size()];
		XYSeriesCollection seriesCollection = new XYSeriesCollection();
		if (seriesList.size() == 1) {
			seriesCollection.addSeries(seriesList.get(0));
			plot.setDataset(1, seriesCollection);
		} else {
			for (int i = 0; i < seriesList.size(); i++) {
				seriesCollection.addSeries(seriesList.get(i));
			}
		}
		renderer.setPlotIcons(plotIcons);
		plot.setDataset(1, seriesCollection);
		chart = new JFreeChart(plot);
		chartPanel = new ChartPanel(chart, true, true, true, true, true);
		chartPanel.setAutoscrolls(true);
		panelTemporario.add(chartPanel);
	}
}
